package com.hpin.fansea.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.GroupResBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/10/22.
 */
public class GoodsListAdapter extends BaseAdapter {

    public DisplayImageOptions options;
    private Context context;
    private List<GroupResBean.GroupResult> groupResults;

    public GoodsListAdapter(Context context, List<GroupResBean.GroupResult> groupResults) {
        this.context = context;
        this.groupResults = groupResults;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_fail_default_list)
                .showImageForEmptyUri(R.mipmap.icon_fail_default_list)
                .showImageOnFail(R.mipmap.icon_fail_default_list)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    public void setGroupResults(List<GroupResBean.GroupResult> groupResults) {
        this.groupResults = groupResults;
    }

    @Override
    public int getCount() {
        return groupResults.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView != null && convertView instanceof LinearLayout) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.item_goods_list,
                    null);
            viewHolder = new ViewHolder();
            viewHolder.item_purchase_image = (ImageView) convertView
                    .findViewById(R.id.item_purchase_image);
            viewHolder.item_purchase_title = (TextView) convertView
                    .findViewById(R.id.item_purchase_title);
            viewHolder.item_purchase_content = (TextView) convertView
                    .findViewById(R.id.item_purchase_content);
            viewHolder.item_purchase_salePr = (TextView) convertView
                    .findViewById(R.id.item_purchase_salePr);
            viewHolder.item_purchase_comment = (TextView) convertView
                    .findViewById(R.id.item_purchase_comment);
            viewHolder.item_purchase_tgPr = (TextView) convertView
                    .findViewById(R.id.item_purchase_tgPr);
            convertView.setTag(viewHolder);
        }

        initData(position, viewHolder);

//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, PurchaseGfDetailActivity.class);
//                Bundle bundle = new Bundle();
//                try {
//
//                    PurchaseGflist purchaseGflist = groupResults.get(position);
////					bundle.putSerializable("purchaseGfDetail", purchaseGflist);
//                    intent.putExtra("moduleId", purchaseGflist.getId_());
////					LogUtil.d("TAG", purchaseGflist.toString());

//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                intent.putExtras(bundle);
//                context.startActivity(intent);
//            }
//        });

        return convertView;
    }

    private void initData(final int position, ViewHolder viewHolder) {
        // 标题 !TextUtils.isEmpty(result.getResult())

        if (!TextUtils.isEmpty(groupResults.get(position).name)) {
            viewHolder.item_purchase_title.setText(groupResults
                    .get(position).name);
        } else {
            viewHolder.item_purchase_title.setText("");
        }
        // 内容
        if (!TextUtils.isEmpty(groupResults.get(position).title)) {
            viewHolder.item_purchase_content.setText(groupResults.get(
                    position).title);
        } else {
            viewHolder.item_purchase_content.setText("");
        }
        // 图片
//        if (!TextUtils.isEmpty(groupResults.get(position).fmpica)) {

        ImageLoader.getInstance().displayImage(groupResults.get(position).fmpica, viewHolder.item_purchase_image, options);
//
//            BitmapHelp.getBitmapUtils(context).display(
//                    viewHolder.item_purchase_image,
//                    groupResults.get(position).getFmpica());
//            LogUtil.e("TAG", "团购列表图片:" + groupResults.get(position).getFmpica());
//        } else {
//            viewHolder.item_purchase_image.setImageResource(R.mipmap.icon_fail_default_list);
//
//        }
        // 销售价
        if (!TextUtils.isEmpty(groupResults.get(position).salePr)) {
            viewHolder.item_purchase_salePr.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
            viewHolder.item_purchase_salePr.setText(groupResults.get(position).salePr);
        } else {
            viewHolder.item_purchase_salePr.setText("");
        }

        viewHolder.item_purchase_comment.setText("已售:" + groupResults.get(position).sold);

        // 团购价格
        if (!TextUtils.isEmpty(groupResults.get(position).tgPr)) {
            viewHolder.item_purchase_tgPr.setText(groupResults.get(position).tgPr + "元");
        } else {
            viewHolder.item_purchase_tgPr.setText("");
        }
    }

    private class ViewHolder {
        public ImageView item_purchase_image;
        public TextView item_purchase_title;
        public TextView item_purchase_content;
        public TextView item_purchase_tgPr;
        public TextView item_purchase_salePr;
        public TextView item_purchase_comment;
    }

}
